<template>
  <j-modal
    :title="title"
    :width="1200"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="model" :rules="validatorRules">
        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="title2" label="一级分类">
          <a-select
            v-model="model.classifyParentId"
            placeholder="请选择"
            style="width: 175px"
            option-label-prop="label"
            @change="selectClick"
          >
            <a-select-option v-for="(item, index) in dataForm" :key="index" :value="item.id" :label="item.classifyName">
              <div slot="title" style="display: flex; flex-direction: row; justify-content: space-between">
                <div>{{ item.classifyName }}</div>
              </div>
            </a-select-option>
          </a-select>
        </a-form-model-item>

        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="classifyId" label="二级分类">
          <!-- {{ model.classifyId }}__{{ radiolist }} -->
          <a-radio-group name="radioGroup" v-model="model.classifyId" @change="classifyIdChange">
            <a-radio :value="item.id" v-for="(item, index) in radiolist" :key="index"> {{ item.classifyName }}</a-radio>
          </a-radio-group>
        </a-form-model-item>

        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="title" label="标题">
          <a-input placeholder="请输入标题" v-model="model.title" />
        </a-form-model-item>

        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="price" label="价格">
          <a-input-number v-model="model.price" :min="0" :precision="2" :defaultValue="0" />
        </a-form-model-item>
        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materialAmount" label="下单物料费">
          <a-input-number v-model="model.materialAmount" :min="0" :precision="2" :defaultValue="0" />
        </a-form-model-item>

        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="virNumber" label="虚拟预约人数">
          <a-input-number v-model="model.virNumber" :min="0" :precision="0" :defaultValue="0" />
        </a-form-model-item>

        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="minNumber" label="起购数量">
          <a-input-number v-model="model.minNumber" :min="0" :precision="0" :defaultValue="0" />
        </a-form-model-item>

        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="timeLong" label="项目时长">
          <a-input-number v-model="model.timeLong" :min="0" :precision="0" :defaultValue="0" />
        </a-form-model-item>
        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="addTimeLong" label="加钟时长">
          <a-input-number v-model="model.addTimeLong" :min="0" :precision="0" :defaultValue="0" />
        </a-form-model-item>


        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="timeType" label="单位">
          <a-radio-group name="radioGroup" v-model="model.timeType" @change="timeTypeChange">
            <a-radio :value="0">分钟</a-radio>
            <a-radio :value="1">小时</a-radio>
            <a-radio :value="2">天</a-radio>
            <a-radio :value="3">月</a-radio>
            <a-radio :value="4">台</a-radio>
            <a-radio :value="5">次</a-radio>
            <a-radio :value="6">平米</a-radio>
            <a-radio :value="7">件</a-radio>
            <a-radio :value="8">个</a-radio>
          </a-radio-group>
        </a-form-model-item>

        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="status" label="状态">
          <a-switch v-model="statusFlag" @change="statusClick()" />
        </a-form-model-item>
        <!-- <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="ifUpgradePatch" label="是否为升级包">
          <a-switch v-model="ifUpgradePatchFlag" @change="ifUpgradePatchClick()" />
        </a-form-model-item> -->
        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="putawayStatus" label="项目上架状态">
          <a-switch v-model="putawayStatusFlag" @change="putawayStatusClick()" />
        </a-form-model-item>
        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="briefIntroduction" label="项目简介">
          <a-textarea v-model="model.briefIntroduction" placeholder="项目简介" :rows="4" />
        </a-form-model-item>
        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="introduce" label="项目介绍">
          <j-editor v-model="model.introduce" />
        </a-form-model-item>
<!--        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="serverExplain" label="说明">-->
<!--          <j-editor v-model="model.serverExplain" />-->
<!--        </a-form-model-item>-->
        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="notice" label="下单须知">
          <j-editor v-model="model.notice" />
        </a-form-model-item>

        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="imgs" label="轮播图">
          <j-image-upload
            class="avatar-uploader"
            text="轮播图"
            v-model="model.imgs"
            isMultiple
            bizPath="5"
          ></j-image-upload>
        </a-form-model-item>

        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="cover" label="封面图">
          <j-image-upload class="avatar-uploader" text="封面图" v-model="model.cover"></j-image-upload>
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </j-modal>
</template>

<script>
import { httpAction } from '@/api/manage'
import { getAction } from '@/api/manage'
import moment from 'moment'
import serveSelect from '@/components/serveSelectx.vue'
// import JDictSelectTag from '@/components/dict/JDictSelectTag'

import { mapState } from 'vuex'
export default {
  name: 'XhdjServerInfoModal',
  components: {
    serveSelect
    // JDictSelectTag
  },
  data () {
    return {
      title: '操作',
      visible: false,
      statusFlag: false, // 状态值
      putawayStatusFlag: false, // 是否上架
      ifUpgradePatchFlag: false,
      dataForm: [],
      radiolist: [],
      model: {
        // classifyParentId: -1
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {
        title: [{ required: true, message: '请填写标题人', trigger: 'change' }],
        price: [{ required: true, message: '请输入价格', trigger: 'change' }],
        timeLong: [{ required: true, message: '请输入时长', trigger: 'change' }]
      },
      url: {
        add: '/xhdjServerInfo/add',
        edit: '/xhdjServerInfo/edit'
      }
    }
  },
  computed: {
    ...mapState({
      agencyCode: state => state.user.agencyCode
    })
  },
  created () {
    this.init()
  },
  methods: {
    init () {
      getAction('/server/xhdjClassifyName/list', {
        pageNo: 1,
        pageSize: 10,
        parentId: 0
      }).then(res => {
        if (res.code === 200) {
          this.dataForm = res.result.records
          console.log(this.model.classifyParentId, '46164')
        }
      })
    },
    add () {
      //初始化默认值
      this.edit({})
    },
    edit (record) {
      this.model = Object.assign({}, record)
      this.putawayStatusFlag = this.isAdminAndstatus(this.model.putawayStatus)
      this.statusFlag = this.isAdminAndstatus(this.model.status)
      this.ifUpgradePatchFlag = this.isAdminAndstatus(this.model.ifUpgradePatch)
      this.visible = true
      this.getTwoList(this.model.classifyParentId)
    },
    close () {
      this.$emit('close')
      this.visible = false
      this.statusFlag = false
      this.ifUpgradePatchFlag = false
      this.$refs.form.clearValidate()
    },
    // 状态
    statusClick () {
      if (this.statusFlag) {
        this.model.status = 1
      } else {
        this.model.status = 0
      }
    },
    // 状态
    ifUpgradePatchClick () {
      if (this.ifUpgradePatchFlag) {
        this.model.ifUpgradePatch = 1
      } else {
        this.model.ifUpgradePatch = 0
      }
    },
    // 编辑代理和状态
    isAdminAndstatus (value) {
      if (value) {
        return true
      } else {
        return false
      }
    },
    putawayStatusClick () {
      if (this.putawayStatusFlag) {
        this.model.putawayStatus = 1
      } else {
        this.model.putawayStatus = 0
      }
    },
    //
    handleOk () {
      console.log(this.model)
      // return
      const that = this
      this.model.agencyCode = this.agencyCode
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method)
            .then(res => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
            })
        } else {
          return false
        }
      })
    },
    handleCancel () {
      this.close()
    },

    selectClick (item) {
      this.getTwoList(item)
      this.model.classifyParentId = item
    },
    getTwoList (value) {
      console.log('7892655')
      getAction('/server/xhdjClassifyName/list', {
        pageNo: 1,
        pageSize: 10,
        parentId: value
      }).then(res => {
        if (res.code === 200) {
          console.log(res, 13)
          this.radiolist = res.result.records
          // this.dataForm = res.result.records
        }
      })
    },
    classifyIdChange (item) {
      console.log(item.target, '446464')
      this.model.classifyId = item.target.value
    },
    timeTypeChange (item) {
      this.model.timeType = item.target.value
    }
  }
}
</script>

<style lang="less" scoped></style>
